package com.yingxin.yxerp.module.appversion.manager;

import com.google.gson.Gson;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AppPushUtils {
    /**
     * push
     * @param
     * @param map title 标题，content 内容
     * @throws IOException
     */
    public static void httpPush(List<String> cids, Map<String,String> map) throws IOException {
        // 1、创建HttpClients对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        //２、创建httpPost对象，设置请求url地址
        HttpPost httpPost = new HttpPost("https://env-00jxhar6rhmf.dev-hz.cloudbasefunction.cn/pushCloud/cloudPush");
        //设置Content-Type请求头
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

        //声明HashMap，封装表单参数search的value
        HashMap<String, Object> searchValueMap = new HashMap<>();
        //手机Id
        searchValueMap.put("push_clientid", cids);

        // 标题
        searchValueMap.put("title", map.get("title"));

        // 内容
        searchValueMap.put("content", map.get("content"));

        // 参数
        HashMap<String, Object> payload = new HashMap<>();
        payload.put("aaa", "bbb");
        searchValueMap.put("payload", payload);

        // 离线配置
        HashMap<String, Object> options = new HashMap<>();
        // oppo 通道id
        HashMap<String, Object> oppo = new HashMap<>();
        oppo.put("/channel_id", "778899"); // oppo消息通道Id
        options.put("OP", oppo);
        // 华为
        HashMap<String, Object> hw = new HashMap<>();
        hw.put("/message/android/category", "WORK");
        hw.put("/message/android/notification/badge/class", "io.dcloud.PandoraEntry");
        hw.put("/message/android/notification/badge/add_num", 1);
        hw.put("/message/android/notification/importance", "NORMAL"); // 华为消息分类
        options.put("HW", hw);
        // 小米
        HashMap<String, Object> xm = new HashMap<>();
        xm.put("/extra.channel_id","125503");
        options.put("XM",xm);

        // vivo
        HashMap<Object, Object> vivo = new HashMap<>();
        vivo.put("/notifyType",2);
        vivo.put("/extra/callback.id",5131);
        //vivo.put("/category",2);
        options.put("VV",vivo);

        // 荣耀
        HashMap<Object, Object> HO = new HashMap<>();
        HO.put("/android/notification/importance","NORMAL");
        HO.put("/android/notification/badge/addNum",1);
        options.put("HO",HO);

        // 魅族不需要配置

        searchValueMap.put("options", options);
        // 服务端推送必须加，推送后客户端自动生成弹出消息
        searchValueMap.put("force_notification", true);

        // 请求唯一
        searchValueMap.put("request_id", String.valueOf(new Date().getTime()));

        //角标
        searchValueMap.put("badge", "+1");

        //申明一个Gson
        Gson gson = new Gson();
        //将表单参数转换成json再转换成字符串
        String searchJson = gson.toJson(searchValueMap);
        //创建jsonEntity，供httpPost使用
        StringEntity jsonEntity = new StringEntity(searchJson, ContentType.APPLICATION_JSON);

        //设置表单的Entity对象到Post请求中
        httpPost.setEntity(jsonEntity);

        //3、使用HttpClient发起请求，获取response
        CloseableHttpResponse response = httpclient.execute(httpPost);
        //4、解析响应
        if (response.getStatusLine().getStatusCode() == 200) {
            String content = EntityUtils.toString(response.getEntity(), "UTF-8");
            System.out.println(content);
        }
        response.close();
        httpclient.close();
    }

}
